﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FightTheDragon.States;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FightTheDragon.Managers
{
    class StateManager
    {
        private Stack<BaseState> m_Stack;
        private static StateManager m_Instance = new StateManager();

        public static StateManager Instance
        {
            get { return m_Instance; }
        }

        private StateManager()
        {
            m_Stack = new Stack<BaseState>();
            
        }

        public void ChangeState(BaseState B)
        {
            if(B != null)
            {
                m_Stack.Peek().Exit();

                B.Enter();

                m_Stack.Push(B);
            }
        }
        public void PushState(BaseState B)
        {
            if (B != null)
            {   
                m_Stack.Push(B);
                m_Stack.Peek().Enter();
            }
        }
        //Special Case for Pause to keep game play in currentState
        public void PopAndResumeState()
        {
            if (m_Stack.Count > 0)
            {
                m_Stack.Peek().Exit();
                m_Stack.Pop();
            }

        }
        public void PopState()
        {
            if (m_Stack.Count > 0)
            {
                m_Stack.Peek().Exit();
                m_Stack.Pop();
                m_Stack.Peek().Enter();
                
            }
        }
        public void RunState(float gTime)
        {
            if (m_Stack.Count > 0)
            {
                m_Stack.Peek().Update(gTime);
                GlobalContentManager.Instance.Graphics.GraphicsDevice.Clear(Color.Black);

                GlobalContentManager.Instance.Sprites.Begin();
                m_Stack.Peek().Render();
                GlobalContentManager.Instance.Sprites.End();
                m_Stack.Peek().HandleInput();
                
                

            }
           
        }
        public void ClearAllStates()
        {
            m_Stack.Clear();
        }
        public void ClearAllandEnter(BaseState B)
        {
            m_Stack.Peek().Exit();
            m_Stack.Clear();
            m_Stack.Push(B);
            m_Stack.Peek().Enter();
        }
    }
}
